package com.group.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group.entity.BarCodReplaceEntity;
import com.group.exception.IndustryException;
import com.group.mapper.BarCodMapper;
import com.group.service.BarCodService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BarCodServiceImpl extends ServiceImpl<BarCodMapper, BarCodReplaceEntity> implements BarCodService {

    @Override
    public boolean legitimateBarCod(String cod) {
        //截取所有条码的前缀和后缀
        List<BarCodReplaceEntity> barCodReplaceEntities = this.baseMapper.selectList(null);

        for (BarCodReplaceEntity barCodReplaceEntity : barCodReplaceEntities) {
            String sourceBarCod = barCodReplaceEntity.getSourceBarCod();
            Integer sourceBarBeginCount = barCodReplaceEntity.getSourceBarBeginCount();
            Integer sourceBarEndCount = barCodReplaceEntity.getSourceBarEndCount();
            //条码前缀相同
            if (cod.substring(sourceBarBeginCount,sourceBarBeginCount+1).
                    equals(sourceBarCod.substring(sourceBarEndCount,sourceBarEndCount+1))){
                //判断后缀
                if (cod.substring(sourceBarEndCount).equals(sourceBarCod.substring(sourceBarEndCount))){
                    return true;
                }
            }

        }
        return false;
    }

    @Override
    public String conversionBarCod(String cod) {

        List<BarCodReplaceEntity> all = this.findAll();
        BarCodReplaceEntity sourceCod = null;
        for (BarCodReplaceEntity barCodReplaceEntity : all) {
            String sourceBarCod = barCodReplaceEntity.getSourceBarCod();
            Integer sourceBarBeginCount = barCodReplaceEntity.getSourceBarBeginCount();
            String substring = sourceBarCod.substring(0, sourceBarBeginCount);
            if (cod.indexOf(substring)!=-1){
                sourceCod=barCodReplaceEntity;
                break;
            }
        }
        if (sourceCod==null)
        throw new IndustryException("不存在的条码信息");
        //开始转换条码信息
        String sourceBarCod = sourceCod.getSourceBarCod();
        Integer sourceBarBeginCount = sourceCod.getSourceBarBeginCount();
        Integer sourceBarEndCount = sourceCod.getSourceBarEndCount();

        String targetBarCod = sourceCod.getTargetBarCod();
        Integer targetBarBeginCount = sourceCod.getTargetBarBeginCount();
        Integer targetBarEndCount = sourceCod.getTargetBarEndCount();
        //抓换条码
        String noHeaderBarCod = cod.substring(cod.length()-sourceBarBeginCount);
        String noTailBarCod = noHeaderBarCod.substring(0, noHeaderBarCod.length() - sourceBarEndCount);

        //条码截取完成
        return targetBarCod.substring(0,targetBarBeginCount)+noTailBarCod+targetBarCod.substring(targetBarEndCount);
    }

    /**
     * 查找所有的条码转换器
     * @return
     */
    @Override
    public List<BarCodReplaceEntity> findAll() {
        return this.baseMapper.selectList(null);
    }
}
